package com.hktf.moedokilock.activity;

import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.hktf.corelib.jsonbean.UserDto;
import com.hktf.corelib.jsonbean.YaoQingRecord;
import com.hktf.corelib.jsonbean.response.LoginResult;
import com.hktf.corelib.jsonbean.response.YaoQingRecordResult;
import com.hktf.corelib.utils.CommUtils;
import com.hktf.corelib.utils.Config;
import com.hktf.corelib.utils.UIUtils;
import com.hktf.moedokilock.R;
import com.hktf.uilib.app.BaseActivity;
import com.hktf.uilib.callback.BaseCallBackNew;
import com.hktf.uilib.engine.DriverEngine;
import com.hktf.uilib.utils.DlgUtils;
import com.hktf.uilib.utils.ShareUtil;
import com.hktf.uilib.utils.ToastUtils;
import com.lzy.okgo.model.Response;

import org.apache.commons.lang3.StringUtils;

import java.util.List;

public class YaoQingActivity extends BaseActivity implements
        OnClickListener {


    private TextView tvSubmit;
    private TextView tvUserid;
    private TextView tvCopy;
    private TextView tvShare;
    private ProgressBar pb;
    private TextView tvCount;
    private android.widget.EditText etCode;

    @Override
    public int getLayoutResID() {
        return R.layout.act_yaoqin;
    }


    @Override
    public void initListener() {

    }

    @Override
    public void initData() {
        UserDto userinfo = Config.USERINFO;
        if (userinfo == null) {
            return;
        }
        tvUserid.setText(userinfo.getUserId());

        getYaoQingRecord();

    }

    private void fillData(){
        tvCount.setText(count+"人");
        int progress=0;
        switch (count) {
            case 0:
                progress=0;
                break;
            case 1:
                progress=19;
                break;
            case 2:
                progress=37;
                break;
            case 3:
                progress=45;
                break;
            case 4:
                progress=53;
                break;
            case 5:
                progress=63;
                break;
            case 6:
                progress=70;
                break;
            case 7:
                progress=77;
                break;
            case 8:
                progress=84;
                break;
            case 9:
                progress=92;
                break;
            case 10:
                progress=100;
                break;
        }
        pb.setProgress(progress);
    }


    @Override
    public void onClick(View v) {
        int id = v.getId();
        CommUtils.closeInput(v);
    }


    public void onDestroy() {
        super.onDestroy();
        try {
            CommUtils.closeInput(etCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private TextView tvRight;


    public void initView() {
        setTitle(UIUtils.getString(com.hktf.uilib.R.string.yaoqing_and_jianli));
        setBackClick();


        tvRight=findViewById(R.id.tv_right);
        tvRight.setText(UIUtils.getString(com.hktf.uilib.R.string.yaoqing_record));
        tvRight.setVisibility(View.VISIBLE);
        tvSubmit = findViewById(R.id.tv_submit);
        tvUserid = findViewById(R.id.tv_userid);
        tvCopy = findViewById(R.id.tv_copy);
        tvShare = findViewById(R.id.tv_share);
        pb = findViewById(R.id.pb);
        tvCount = findViewById(R.id.tv_count);
        pb.setProgress(0);

        tvCopy.setOnClickListener(v -> {
            CommUtils.copy(Config.USERINFO.getUserId(), YaoQingActivity.this);
        });
        etCode = findViewById(R.id.et_code);
        tvRight.setOnClickListener(v -> {
            startActivity(new Intent(YaoQingActivity.this, YaoQingRecordActivity.class));
        });
        tvSubmit.setOnClickListener(this::submit);
        tvShare.setOnClickListener(this::share);
    }
    private void share(View v){
        String msg="打开萌动锁屏，点击我的板块，找到邀请与奖励，输入邀请码：" +Config.USERINFO.getUserId()+
                ",点击提交按钮提交成功后获赠7天会员。萌动锁屏，是一款简洁易用的锁屏壁纸软件，可在各应用市场搜索下载。";
        ShareUtil shareUtil=new ShareUtil(YaoQingActivity.this);
        shareUtil.shareText(null, null, msg, CommUtils.getString(R.string.app_name) + "分享", null);
    }
    private void submit(View v) {
//        if(true){
//            count++;
//            fillData();
//            return;
//        }
        CommUtils.closeInput(etCode);
        String code = etCode.getText().toString();
        if (StringUtils.isEmpty(code)) {
            ToastUtils.showToast(this, UIUtils.getString(com.hktf.uilib.R.string.yaoqing_hint1));
            return;
        }

        if(code==Config.USERINFO.getUserId()){
            ToastUtils.showToast(this, UIUtils.getString(com.hktf.uilib.R.string.yaoqing_hint2));
            return;
        }


        showLoadingSend();
        DriverEngine.yaoqingSubmit(code, new BaseCallBackNew<LoginResult>(LoginResult.class) {
            @Override
            public void onSUCCESS(Response<LoginResult> response) {
                super.onSUCCESS(response);
                LoginResult body = response.body();
                UserDto data = body.getData();
                CommUtils.saveUserInfo(data);
            }
        });
    }
    private int count;

    private void getYaoQingRecord(){
        showLoadingGet();
        DriverEngine.yaoqingList(new BaseCallBackNew<YaoQingRecordResult>(YaoQingRecordResult.class) {
            @Override
            public void onSUCCESS(Response<YaoQingRecordResult> response) {
                DlgUtils.disMissDlg();
                YaoQingRecordResult body = response.body();
                List<YaoQingRecord> data = body.getData();
                if(data !=null){
                    count=data.size();
                    fillData();
                }
            }
        });
    }








    @Override
    protected void onResume() {
        super.onResume();

    }



    @Override
    protected void onPause() {
        super.onPause();

    }





}
